package com.bringway.p2p.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ZctztjDao;

import net.sf.json.JSONObject;

/**
 * @author js
 * @version 创建时间：2016年6月29日
 * @功能: 每日注册投资信息统计界面
 */
@Repository
public class ZctztjDaoImpl extends P2PJdbcDao implements ZctztjDao {
	Logger logger = Logger.getLogger(ZctztjDaoImpl.class);

	/**
	 * @author js ZctztjDao的实现类 获取每日注册投资统计列表
	 */
	@Override
	public List<Map<String, Object>> queryZctztjList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> list=new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select date_format(:queryDate,'%Y-%m-%d') CXRQ,");
		sql.append(
				"ifnull((select count(*) ZCCG from t_zhfw_user where DISABLED = '1' AND date_format(cjrq,'%Y-%m-%d') =:queryDate group by date_format( cjrq,'%Y-%m-%d')),0) ZCCG,");
		sql.append(
				"ifnull((select count(*) XTJR from t_zhfw_user where DISABLED = '1' AND date_format(cjrq,'%Y-%m-%d') =:queryDate AND tjr is not null and tjr <>'' group by date_format( cjrq,'%Y-%m-%d')),0) XTJR,");
		sql.append(
				"ifnull((select count(*) TZSL from t_zhfw_tzrxx where date_format(tzsj,'%Y-%m-%d') =:queryDate AND TCZT='成功' group by date_format(tzsj,'%Y-%m-%d')),0) TZSL,");
		sql.append(
				"ifnull((select count(*) CVIP from t_zhfw_vipcz where date_format(czsj,'%Y-%m-%d') =:queryDate group by date_format(czsj,'%Y-%m-%d')),0) CVIP,");
		sql.append(
				"ifnull((select max(TCJE) MAX from t_zhfw_tzrxx where date_format(tzsj,'%Y-%m-%d') =:queryDate AND TCZT='成功'),0) MAX");
		sql.append(" FROM dual");
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Date minDate=new Date();
		Date maxDate=new Date();
		try {
			minDate = sf.parse(startDate);
			maxDate = sf.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		for (int i = start; i < (start + limit); i++) {
			calendar.setTime(maxDate);
			calendar.add(Calendar.DATE, -i);
			Date queryDate=calendar.getTime();
			if (queryDate.before(minDate)) {
				break;
			}
			paramMap.put("queryDate", sf.format(queryDate));
			//logger.info("queryZctztjList paramMap is : " + paramMap);
			Map<String, Object> result=this.getNamedParameterJdbcTemplate().queryForMap(sql.toString(), paramMap);
			list.add(result);		
		}
		//logger.info("queryZctztjList list is : " + list.size());
		return list;
	}

	@Override
	public int getZctztjCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Date minDate=new Date();
		Date maxDate=new Date();
		try {
			minDate = sf.parse(startDate);
			maxDate = sf.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		int i=0;
		Date queryDate=minDate;
		do {
			calendar.setTime(minDate);
			calendar.add(Calendar.DATE, i++);
			queryDate=calendar.getTime();
		} while (queryDate.before(maxDate));
		return i;
	}

	@Override
	public List<Map<String, Object>> queryGrtz(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String tzsj = params.optString("tzsj");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		StringBuffer sql = new StringBuffer();
		sql.append(
				" select USERID,TCJE,date_format(TZSJ,'%Y-%m-%d %H:%i:%s') TZSJ,TCZT,ZQID,HTBH from t_zhfw_tzrxx");
		sql.append(" where 1=1");
		if (StringUtils.isNotEmpty(tzsj)) {
			sql.append(" AND date_format(TZSJ,'%Y-%m-%d') = :TZSJ ");
			paramMap.put("TZSJ", tzsj);
		}
		sql.append(" order by TZSJ desc LIMIT :START,:LIMIT");
		logger.info("queryGrtz sql is : " + sql.toString());
		logger.info("queryGrtz paramMap is : " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

	@Override
	public int getGrtzCount(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String tzsj = params.optString("tzsj");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(*) from t_zhfw_tzrxx");
		sql.append(" where 1=1");
		if (StringUtils.isNotEmpty(tzsj)) {
			sql.append(" AND date_format(TZSJ,'%Y-%m-%d') = :TZSJ ");
			paramMap.put("TZSJ", tzsj);
		}
		logger.info("getGrtzCount sql is : " + sql.toString());
		logger.info("getGrtzCount paramMap is : " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public float getGrtzJe(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String tzsj = params.optString("tzsj");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		StringBuffer sql = new StringBuffer();
		sql.append(" select ifnull(sum(TCJE),0) from t_zhfw_tzrxx");
		sql.append(" where 1=1");
		if (StringUtils.isNotEmpty(tzsj)) {
			sql.append(" AND date_format(TZSJ,'%Y-%m-%d') = :TZSJ ");
			paramMap.put("TZSJ", tzsj);
		}
		logger.info("getGrtzJe sql is : " + sql.toString());
		logger.info("getGrtzJe paramMap is : " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, Float.class);
	}
}
